package com.topscomm.file.mq;

import java.io.IOException;
import java.util.Map;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

/**
 * 文件处理消息消费者
 * 
 * @description:
 * @author: donghang
 * @date: 2020年8月20日下午2:15:13
 * @modify:
 */
@Component
@Slf4j
@RabbitListener(queues = "${rabbitmq.fileprocess.queuename}")
public class FileProcessConsumer {
	/**
	 * 
	 * 
	 * @description:
	 * @author: donghang
	 * @date: 2020年8月20日下午3:25:51
	 * @modify:
	 */
	public FileProcessConsumer() {

	}

	/**
	 * 消息线程池处理
	 * 
	 * @description:
	 * @param notification
	 * @author: donghang
	 * @throws IOException
	 * @date: 2020年8月20日下午2:16:42
	 * @modify:
	 */
	@RabbitHandler
	public void dispatch(@Payload Map<String, Object> notification, Message message, Channel channel)
			throws IOException {
		log.info("consume mq message : " + notification.toString());
		FileProcessTask fileProcessTask = new FileProcessTask(notification);
		try {
			fileProcessTask.call();
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
		} catch (Exception e) {
			log.info("consume mq message : " + notification.toString() + "异常，异常信息" + e.getMessage());
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
			// requeue =true 重回队列，false 丢弃
			// channel.basicReject(message.getMessageProperties().getDeliveryTag(),
			// true);
		}
	}

}
